package com.bwei.topnews.fragment;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.bwei.topnews.LocalCityA;
import com.bwei.topnews.R;
import com.bwei.topnews.adapter.CategoryAdapter;
import com.bwei.topnews.base.BaseData;
import com.bwei.topnews.base.BaseFragment;
import com.bwei.topnews.bean.NewsCategoryRoot;
import com.bwei.topnews.utils.URLUtils;
import com.google.gson.Gson;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qwe on 2016/11/15.
 */
public class LocalCityFragment extends BaseFragment implements SpringView.OnFreshListener, View.OnClickListener {

    private TextView tv_fragment_city_selector;
    private ListView lv_fragment_city;
    List<String> list = new ArrayList<>();
    private SpringView springview;
    private CategoryAdapter categoryAdapter;

    @Override
    protected View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_city_selector, null);
        springview = (SpringView) view.findViewById(R.id.springview);
        tv_fragment_city_selector = (TextView) view.findViewById(R.id.tv_fragment_city_selector);
        lv_fragment_city = (ListView) view.findViewById(R.id.lv_fragment_city);
        // 添加头和尾
        springview.setHeader(new DefaultHeader(getActivity()));
        springview.setFooter(new DefaultHeader(getActivity()));
        springview.setListener(this);
        springview.setType(SpringView.Type.FOLLOW);
        tv_fragment_city_selector.setOnClickListener(this);
        return view;
    }

    @Override
    protected void initActivityCreated() {

        initeCity("北京");

    }


    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadmore() {

    }

    @Override
    public void onClick(View v) {

        Intent in = new Intent(getActivity(), LocalCityA.class);
        getActivity().startActivityForResult(in,100);

    }

    public void initeCity(String city){
        BaseData baseData = new BaseData() {
            @Override
            public void setResultData(String data) {
                Gson  gson = new Gson();
                NewsCategoryRoot newsCategoryRoot = gson.fromJson(data, NewsCategoryRoot.class);
                List<NewsCategoryRoot.DataBean> data1 = newsCategoryRoot.getData();
                if(categoryAdapter==null) {
                    categoryAdapter = new CategoryAdapter(data1, getActivity());
                    lv_fragment_city.setAdapter(categoryAdapter);
                }else{
                    categoryAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void setFailResult(int error_Net) {

            }
        };
        baseData.getData(URLUtils.URL_CATEGROY_PATH+"&user_city"+city,BaseData.SHORTTIME);
    }

    @Override
    public void onStop() {
        super.onStop();
        categoryAdapter=null;
    }
}
